package com.ht.controller;

import com.alibaba.druid.stat.TableStat;
import com.alibaba.fastjson.JSON;
import com.ht.bean.*;
import com.ht.dao.UserDao;
import com.ht.service.HouqingService;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/houqing")
public class Houqingcontroller {

    @Resource
    private HouqingService hs;

    //维修管理查询
    @RequestMapping("/weixiutolist")
    public String weixiutolist(){
        return "weixiutolist";
    }


    ////维修管理查询
    @RequestMapping("/weixiulist")
    @ResponseBody
    public Map weixiulist(Integer page, Integer limit, Equipmentrepair equipmentrepair, HttpSession session,Model model){//获取layui表格传递过来的分页参数（注意：参数名称不能修改）
        List<Map> eplist=hs.eplist((page-1)*limit,limit,equipmentrepair);

        //模拟登录所带的值
//        session.setAttribute("student","1");


        System.out.println(eplist.toString());
        Map map=new HashMap();

//        model.addAttribute("jinyong",jinyong);
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",hs.countep(equipmentrepair));//总页数
        map.put("data",eplist);//数据
        return map;
    }

    //维修管理 已完成 和维修状态转换
    @RequestMapping("/weixiudel")
    @ResponseBody
    public Map del(Integer equipmentId){
        Map map=new HashMap();
        Equipmentrepair equipmentrepair=new Equipmentrepair();

        //获取id
        equipmentrepair.setEquipmentId(equipmentId);

        //转换状态
        equipmentrepair.setStatus(1);

        //获取当前时间转换成string类型
//        Date currentTime = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String dateString = formatter.format(currentTime);

//        equipmentrepair.setEndTime(dateString);



        boolean bln=hs.weixiushijian(equipmentrepair);
        if (bln){
            map.put("msg","已完成");
        }else {
            map.put("msg","完成失败");
        }
        return map;
    }




    //维修已完成状态
    @RequestMapping("/weixiushanchu")
    @ResponseBody
    public Map shanchu(String equipmentId){
        Map map=new HashMap();
        boolean bln=hs.weixiudel(equipmentId);
        if (bln){
            map.put("msg","已完成");
        }else {
            map.put("msg","完成失败");
        }
        return map;
    }




    //维修管理新增
    @RequestMapping("/weixiutoadd")
    public String weixiutoadd(Student student,Model model,HttpSession session){
        List<Studentclass>sclist=hs.classlist();
        List<Student>listsd=hs.listsd();
        List<Dep>deplist=hs.deplist();
        model.addAttribute("listsd",listsd);
        model.addAttribute("sclist",sclist);
        model.addAttribute("deplist",deplist);
        return "weixiuadd";
    }

    //维修管理新增
    @RequestMapping("/weixiuadd")
    @ResponseBody
    public Map weixiuadd(Integer equipmentId,Equipmentrepair equipmentrepair,HttpSession session){
        Map map=new HashMap();

//        //获取id
        equipmentrepair.setEquipmentId(equipmentId);
//
//        //转换状态
        equipmentrepair.setStatus(0);

        boolean bln=hs.weixiuadd(equipmentrepair);
        if(bln){
            map.put("mag","申请成功");
        }else{
            map.put("mag","申请失败");
        }
        return map;
    }

    //维修管理修改
    @RequestMapping("/weixiutoupd/{equipmentId}")
    public String weixiutoupd(@PathVariable("equipmentId") String equipmentId, Model model){
        Equipmentrepair ep=hs.weixiusel(equipmentId);
        model.addAttribute("ep", JSON.toJSON(ep));//将对象转换成json对象


        //查询学生班级表
        List<Studentclass>sclist=hs.classlist();

        //查询部门表
        List<Dep>deplist=hs.deplist();

        //查询学生表
        List<Student> listsd=hs.listsd();

        model.addAttribute("listsd",listsd);
        model.addAttribute("sclist",sclist);
        model.addAttribute("deplist",deplist);

        return "weixiuupd";
    }

    //维修管理修改
    @RequestMapping("/weixiuupd")
    @ResponseBody
    public Map weixiuupd(Equipmentrepair equipmentrepair){
        Map map=new HashMap();
        boolean bln=hs.weixiuupd(equipmentrepair);
        if(bln){
            map.put("mag","修改成功");
        }else{
            map.put("mag","修改失败");
        }
        return map;
    }



    //楼栋维护👇↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓------------------------------------------------------------------------------------------------------------------------------------

    //楼栋管理查询
    @RequestMapping("/loudongtolist")
    public String loudongtolist(){
        return "loudonglist";
    }

    //楼栋管理查询
    @RequestMapping("/loudonglist")
    @ResponseBody
    public Map loudonglist(Integer page,Integer limit, Studentfloor studentfloor){
        List<Map> ldlist=hs.loudonglist((page-1)*limit,limit,studentfloor);
        System.out.println(ldlist.toString());
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",hs.loudongcount(studentfloor));//总页数
        map.put("data",ldlist);//数据
        return map;
    }

    //楼栋管理删除
    @RequestMapping("/loudongdel")
    @ResponseBody
    public Map loudongdel(String floorId){
        Map map=new HashMap();
        boolean bln=hs.loudongdel(floorId);
        if (bln){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }



    //楼栋管理新增
    @RequestMapping("/loudongtoadd")
    public String loudongtoadd(){

        return "loudongadd";
    }

    //楼栋管理新增.
    @RequestMapping("/loudongadd")
    @ResponseBody
    public Map loudongadd(Integer floorId,Studentfloor studentfloor,HttpSession session){
        Map map=new HashMap();
        boolean bln=hs.loudongadd(studentfloor);
        if(bln){
            map.put("mag","添加成功");
        }else{
            map.put("mag","添加失败");
        }
        return map;
    }



    //维修管理修改
    @RequestMapping("/loudongtoupd/{floorId}")
    public String loudongtoupd(@PathVariable("floorId") String floorId, Model model){
        Studentfloor sf=hs.studentfloor(floorId);
        model.addAttribute("sf", JSON.toJSON(sf));//将对象转换成json对象
        return "loudongupd";
    }

    //维修管理修改
    @RequestMapping("/loudongupd")
    @ResponseBody
    public Map loudongupd(Studentfloor studentfloor){
        Map map=new HashMap();
        boolean bln=hs.loudongupd(studentfloor);
        if(bln){
            map.put("mag","修改成功");
        }else{
            map.put("mag","修改失败");
        }
        return map;
    }




    //宿舍管理↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------+-+-+-+-+--+----------------++++++++++++++++++++++----------------+++++++++++++++++-------------------++++++++

    //宿舍管理查询
    @RequestMapping("/sushetolist")
    public String sushetolist(){
        return "sushelist";
    }

    //宿舍管理查询
    @RequestMapping("/sushelist")
    @ResponseBody
    public Map sushelist(Integer page,Integer limit, Studenthuor studenthuor){
        List<Map> sushelist=hs.sushelist((page-1)*limit,limit,studenthuor);
        System.out.println(sushelist.toString());
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",hs.sushecount(studenthuor));//总页数
        map.put("data",sushelist);//数据
        return map;
    }

    //宿舍删除
    @RequestMapping("/sushedel")
    @ResponseBody
    public Map susheshanchu(String huorId){
        Map map=new HashMap();
        boolean bln=hs.sushedel(huorId);
        if (bln){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }

    //宿舍添加
    @RequestMapping("/sushetoadd")
    public String sushetoadd(Model model){
        List<Studentfloor>sflist=hs.sflist();
        model.addAttribute("sflist",sflist);
        return "susheadd";
    }

    //宿舍添加
    @RequestMapping("/susheadd")
    @ResponseBody
    public Map susheadd(Studenthuor studenthuor){

        Map map=new HashMap();
        boolean bln=hs.susheadd(studenthuor);
        if(bln){
            map.put("mag","添加成功");
        }else{
            map.put("mag","添加失败");
        }
        return map;
    }

    //宿舍管理修改
    @RequestMapping("/sushetoupd/{huorId}")
    public String sushetoupd(@PathVariable("huorId") String huorId, Model model){
        Studenthuor ss=hs.sushesel(huorId);

        model.addAttribute("ss", JSON.toJSON(ss));//将对象转换成json对象

        List<Studentfloor>sflist=hs.sflist();
        model.addAttribute("sflist",sflist);
        return "susheupd";
    }

    //宿舍管理修改
    @RequestMapping("/susheupd")
    @ResponseBody
    public Map susheupd(Studenthuor studenthuor){
        Map map=new HashMap();
        boolean bln=hs.susheupd(studenthuor);
        if(bln){
            map.put("mag","修改成功");
        }else{
            map.put("mag","修改失败");
        }
        return map;
    }

}
